package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @version 1.0
 * @description <p>自媒体图文素材信息 业务实现</p>
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Value("${fdfs.web-server-url}")
    private String fastdfsServer;


    /**
     * 素材分页
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo pageList(WmMaterialPageRequestDto dto) {
 /*       IPage<WmMaterial> pageInfo = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WmMaterial::getUserId, RequestContextUtil.getUserId());
        lqw.eq(null != dto.getIsCollection(), WmMaterial::getIsCollection, dto.getIsCollection());
        page(pageInfo, lqw);

        List<WmMaterial> wmMaterialList = pageInfo.getRecords();
        if (!CollectionUtils.isEmpty(wmMaterialList)) {
            wmMaterialList.forEach(m -> m.setUrl(fastdfsServer + m.getUrl()));
        }
        return PageResultVo.pageResult(dto.getPage(), dto.getSize(), pageInfo.getTotal(), wmMaterialList);
    }
}
*/

        // 1. 设置分页参数
        IPage<WmMaterial> pageInfo = new Page<>(dto.getPage(), dto.getSize());
        // 2. 构建条件
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        // 只能查询登录用户自己的素材
        lqw.eq(WmMaterial::getUserId, RequestContextUtil.getUserId());
        lqw.eq(null != dto.getIsCollection(), WmMaterial::getIsCollection, dto.getIsCollection());
        // 3. 查询
        page(pageInfo, lqw);
        // 3.1 对查询结果处理，设置图片的完整路径
        List<WmMaterial> wmMaterialList = pageInfo.getRecords();
        if(!CollectionUtils.isEmpty(wmMaterialList)){
            // 有素材,补全图片的全路径
            wmMaterialList.forEach(m->m.setUrl(fastdfsServer + m.getUrl()));
            /*for (WmMaterial m : wmMaterialList) {
                m.setUrl(fastdfsServer + m.getUrl());
            }*/
        }
        // 4. 构建pageResult返回
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(), pageInfo.getTotal(), wmMaterialList);
    }
}
